<?php
class sms {

    //云通讯
    static function yuntongxun($to, $datas, $tempId = 50530) {
    	$accountSid = 'aaf98f8950f4a62c0151061744fd43b8';   //快聊 8a216da8552a3cd401554392c49e13ae //企云信 aaf98f8950f4a62c0151061744fd43b8
    	$accountToken = '834b696ff4954450b751a8a4590f1fe0'; //快聊 e3248373fa8e42459381a5acfcd03bcb //企云信 834b696ff4954450b751a8a4590f1fe0
    	$appId = '8a48b55150f4a72601510619144845ad';   //快聊 8a216da8552a3cd401554392c4fd13b4 //企云信 aaf98f89512446e2015128f0802e0d4e
//    	$accountSid = '8aaf0708584c07c20158613bf6bd0c9f';   //快聊 8a216da8552a3cd401554392c49e13ae //企云信 aaf98f8950f4a62c0151061744fd43b8
//    	$accountToken = '957cf08a8ff54b56a493895742ee4635'; //快聊 e3248373fa8e42459381a5acfcd03bcb //企云信 834b696ff4954450b751a8a4590f1fe0
//    	$appId = '8aaf0708584c07c20158613bf8410ca6';   //快聊 8a216da8552a3cd401554392c4fd13b4 //企云信 aaf98f89512446e2015128f0802e0d4e
    	$serverIP = 'app.cloopen.com'; //沙盒环境（用于应用开发调试）：sandboxapp.cloopen.com 生产环境（用户应用上线使用）：app.cloopen.com
    	$serverPort = '8883';
    	$softVersion = '2013-12-26';
    	$Batch = date("YmdHis");
    	$BodyType = 'json';

    	$data = "";
    	for ($i = 0; $i < count($datas); $i++) {
    		$data = $data . "'" . $datas[$i] . "',";
    	}
    	$body = "{'to':'$to','templateId':'$tempId','appId':'$appId','datas':[" . $data . "]}";
    	$sig = strtoupper(md5($accountSid . $accountToken . $Batch));
    	$url = "https://" . $serverIP . ":" . $serverPort . "/" . $softVersion . "/Accounts/" . $accountSid . "/SMS/TemplateSMS?sig=" . $sig;
    	$authen = base64_encode($accountSid . ":" . $Batch);
    	$header = array("Accept:application/$BodyType", "Content-Type:application/$BodyType;charset=utf-8", "Authorization:$authen");
    	$result = \tools::curl_post($url, $body, $header);
    	if ($BodyType == "json") {//JSON格式
    		$datas = json_decode($result);
    	} else { //xml格式
    		$datas = simplexml_load_string(trim($result, " \t\n\r"));
    	}
    
    	if ($datas->statusCode == '000000') {
    		return true;
    	}
    	return false;
    }
    
    
    //轻码
    static function qingma($to, $vercode, $tempId = 23000034) {//21310017
    	$BASE_URL = "https://api.miaodiyun.com/20150822/";
    	$ACCOUNT_SID = "4526d6b8814144659085b96d98956497"; // 主账户
    	$AUTH_TOKEN = "faa533ebb5cf44f4bfd6ca24bd942b20";
    	$CONTENT_TYPE = "application/x-www-form-urlencoded";
    	$ACCEPT = "application/json";
    	
    	$funAndOperate = "industrySMS/sendSMS";
    	
    	date_default_timezone_set("Asia/Shanghai");
    	$timestamp = date("YmdHis");
    	// 签名
    	$sig = md5($ACCOUNT_SID . $AUTH_TOKEN . $timestamp);
    	$body = array("accountSid" => $ACCOUNT_SID, "timestamp" => $timestamp, "sig" => $sig, "respDataType"=> "JSON");
    	
    	// 在基本认证参数的基础上添加短信内容和发送目标号码的参数
    	$body['smsContent'] = "【洪管家】您的验证码是{$vercode}，10分钟内有效。";
    	$body['to'] = $to;
    	
    	// 构造请求数据
    	$url = $BASE_URL . $funAndOperate;
    	$headers = array('Content-type: ' . $CONTENT_TYPE, 'Accept: ' . $ACCEPT);
    	
    	$fields_string = "";
    	foreach ($body as $key => $value) {
    		$fields_string .= $key . '=' . $value . '&';
    	}
    	rtrim($fields_string, '&');
    	
    	// 提交请求
    	$con = curl_init();
    	curl_setopt($con, CURLOPT_URL, $url);
    	curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
    	curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($con, CURLOPT_HEADER, 0);
    	curl_setopt($con, CURLOPT_POST, 1);
    	curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
    	curl_setopt($con, CURLOPT_POSTFIELDS, $fields_string);
    	$result = curl_exec($con);
    	curl_close($con);
    	
        $result=json_decode($result , true);
    	if($result['respCode']=='00000'){
    		return true;
    	}else{
    		return false;
    	}
    	
    	
    	
    	
    	
    	
    	$BASE_URL = "https://api.qingmayun.com/20150822/SMS/templateSMS";
    	
    	$ACCOUNT_SID = 'e38d98c2d0f24ca4ad33a519b940e95f';
    	$KEY = "e47a1c192df84fe9b1b23d00c73f7224";
    	$appId = "32397ace5dd049d1b9262beec79c4f57";//应用唯一标识
    	$CONTENT_TYPE = "application/json";
    	$ACCEPT = "application/json";
    	$param = $vercode;
    	 
    	date_default_timezone_set("Asia/Shanghai");
    	$timestamp = date("YmdHis");
    	
    	// 签名
    	$sig = md5($ACCOUNT_SID.$KEY.$timestamp);
    	
    	$postArr = array (
    			'accountSid' => $ACCOUNT_SID,
    			'appId' => $appId,
    			'templateId' => $tempId,
    			'to' => $to,
    			'param' => $vercode,
    			'timestamp' => $timestamp,
    			'sig' => $sig,
    			'respDataType' => 'JSON'
    	);
    	 
    	$postArr = http_build_query($postArr);
    	$ch = curl_init ();
    	curl_setopt ( $ch, CURLOPT_POST, 1 );
    	curl_setopt ( $ch, CURLOPT_HEADER, 0 );
    	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    	curl_setopt ( $ch, CURLOPT_URL, $BASE_URL );
    	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postArr );
    	$result = curl_exec ( $ch );
    	curl_close ( $ch );
    	 
    	$result=json_decode($result , true);
    	if($result['respCode']=='00000'){
    		return true;
    	}else{
    		return false;
    	}
    	
    	
    	
    	$BASE_URL = "https://api.qingmayun.com/20141029/accounts/";
		$ACCOUNT_SID = 'e38d98c2d0f24ca4ad33a519b940e95f';
		$KEY = "e47a1c192df84fe9b1b23d00c73f7224";
		$appId = "32397ace5dd049d1b9262beec79c4f57";//应用唯一标识
		$CONTENT_TYPE = "application/json";
		$ACCEPT = "application/json";
		$param = $vercode;
    	
		date_default_timezone_set("Asia/Shanghai");
		$timestamp = date("YmdHis");
		
		// 签名
		$sig = md5($ACCOUNT_SID.$KEY.$timestamp);
		$url = $BASE_URL . $ACCOUNT_SID . "/SMS/templateSMS?sig=" . $sig . "&timestamp=" . $timestamp;
		
		
		
		$body = "";
		if($CONTENT_TYPE == "application/json"){
			$body = "{\"templateSMS\": {\"appId\":\"" . $appId . "\",\"templateId\":\"" . $tempId . "\",\"to\":\"" . $to . "\",\"param\":\"" . $param . "\"}}";
		}elseif($CONTENT_TYPE == "application/xml"){
			$body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><templateSMS><appId>" . $appId . "</appId><templateId>" . $tempId . "</templateId><to>" . $to . "</to><param>" . $param . "</param></templateSMS>";
		}else{
			return false;
		}
		
		$headers = array('Content-type: '.$CONTENT_TYPE, 'Accept: '.$ACCEPT, 'Content-Length: '.strlen($body));
		
		$con = curl_init();
		curl_setopt($con, CURLOPT_URL, $url);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt ($con, CURLOPT_HEADER, 0);
		curl_setopt($con, CURLOPT_POST, 1);
		curl_setopt ($con, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($con, CURLOPT_POSTFIELDS, $body);
		$result = curl_exec($con);
		curl_close($con);
		
		$datas = json_decode($result , true);
    	if ($datas['result']['respCode'] == '00000') {
    		return true;
    	}
    	return false;
    }
    
    
    //创蓝
    static function chuanglan($to, $msg, $needstatus = true , $product = "" , $extno = "") {
    	$chuanglan_config['api_send_url'] = 'http://222.73.117.158/msg/HttpBatchSendSM';
    	//$chuanglan_config['api_send_url'] = 'http://222.73.117.158/msg/index.jsp';
    	$chuanglan_config['api_balance_query_url'] = 'http://222.73.117.158/msg/QueryBalance';
    	$chuanglan_config['api_account']	= 'wulinhui';//'jiekou-clcs-12'; wulinhui
    	$chuanglan_config['api_password']	= 'Tch123456';
    	
    	$postArr = array (
    			'account' => $chuanglan_config['api_account'],
    			'pswd' => $chuanglan_config['api_password'],
    			'msg' => $msg,
    			'mobile' => $to,
    			'needstatus' => $needstatus,
    			'product' => $product,
    			'extno' => $extno
    	);
    	
    	$postArr = http_build_query($postArr);
    	$ch = curl_init ();
    	curl_setopt ( $ch, CURLOPT_POST, 1 );
    	curl_setopt ( $ch, CURLOPT_HEADER, 0 );
    	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    	curl_setopt ( $ch, CURLOPT_URL, $chuanglan_config['api_send_url'] );
    	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postArr );
    	$result = curl_exec ( $ch );
    	curl_close ( $ch );
    	
    	$result=preg_split("/[,\r\n]/",$result);
    	if($result[1]==0){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    //迅脉优力迪
    static function xunmai($to, $msg) {
    	$flag = 0;
    	$params='';
    	//要post的数据
    	$argv = array(
    			'sn'=>'DXX-MDQ-010-00008', ////替换成您自己的序列号
    			'pwd'=>strtoupper(md5('DXX-MDQ-010-00008'.'934894')), //此处密码需要加密 加密方式为 md5(sn+password) 32位大写
    			'mobile'=>$to,//手机号 多个用英文的逗号隔开 post理论没有长度限制.推荐群发一次小于等于10000个手机号
    			//'content'=>iconv( "UTF-8", "gb2312//IGNORE" ,"{$add_staff[0]} , 您的验证码是:{$add_staff[1]}. 请不要把验证码泄露给其他人.如非本人操作,可不用理会! [迅脉优力迪]"),//'您好测试,短信测试[签名]',//短信内容
    			'content'=>$msg,//iconv( "UTF-8", "gb2312//IGNORE" ,$msg),
    			'ext'=>'',
    			'stime'=>'',//定时时间 格式为2011-6-29 11:09:21
    			'msgfmt'=>'',
    			'rrid'=>''
    	);
    	//构造要post的字符串
    	//echo $argv['content'];
    	foreach ($argv as $key=>$value) {
    		if ($flag!=0) {
    			$params .= "&";
    			$flag = 1;
    		}
    		$params.= $key."="; $params.= urlencode($value);// urlencode($value);
    		$flag = 1;
    	}
    	$length = strlen($params);
    	//创建socket连接
    	$fp = fsockopen("sdk.entinfo.cn",8061,$errno,$errstr,10) or exit($errstr."--->".$errno);
    	//构造post请求的头
    	$header = "POST /webservice.asmx/mdsmssend HTTP/1.1\r\n";
    	$header .= "Host:sdk.entinfo.cn\r\n";
    	$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    	$header .= "Content-Length: ".$length."\r\n";
    	$header .= "Connection: Close\r\n\r\n";
    	//添加post的字符串
    	$header .= $params."\r\n";
    	fputs($fp,$header);
    	$inheader = 1;
    	while (!feof($fp)) {
    		$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
    		if ($inheader && ($line == "\n" || $line == "\r\n")) {
    			$inheader = 0;
    		}
    	}
    	
    	$line=str_replace("<string xmlns=\"http://tempuri.org/\">","",$line);
    	$line=str_replace("</string>","",$line);
    	$result=explode("-",$line);
    	
    	if(count($result)>1)
    		return false;
    	else
    		return true;
    }
}


